﻿using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class VAP_Details : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("FD1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                    return;
                }
                lblEmdID.Text = Request.QueryString["emd_id"].ToString();
            }

            lblError.Text = "";
            lblErrOri.Text = "";
            lblErrDes.Text = "";
            lblErrCabin.Text = "";
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string note_buf = txtNote.Text.Replace("\n", "<br/>");
            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_add_name");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@lang", Convert.ToInt16(lstLang.SelectedValue));
                para = cmd.Parameters.AddWithValue("@name", txtName.Text);
                para = cmd.Parameters.AddWithValue("@unit", txtUnit.Text);
                para = cmd.Parameters.AddWithValue("@note", note_buf);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Inserting failed! EMD not found, or EMD is not editing.";
                else if (ret_code == 2)
                    lblError.Text = "Inserting failed! Maybe duplicated language.";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_delete_name");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@lang", Convert.ToInt16(Request.Form["KEY"]));
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Deleting failed! EMD not found, or EMD is not editing.";
                else if (ret_code == 2)
                    lblError.Text = "Deleting failed! Maybe the record is not exist.";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnDeleteOri_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrOri.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_delete_ori_airport");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@airport", lstApplyOri.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrOri.Text = "Done.";
                    lstApplyOri.DataBind();
                }
                else if (ret_code == 1)
                    lblErrOri.Text = "The EMD is not in edit mode!";
                else if (ret_code == 2)
                    lblErrOri.Text = "Deleting failed!";
                else
                    lblErrOri.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrOri.Text = ex.Message;
                else
                    lblErrOri.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAddOri_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrOri.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            foreach (ListItem item in lstAirports1.Items)
            {
                if (item.Selected)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("bll_emd_add_ori_airport");
                        SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                        para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                        para = cmd.Parameters.AddWithValue("@airport", item.Value);
                        para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                        para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                        //para = new SqlParameter("@ret_code", SqlDbType.Int);
                        //para.Direction = ParameterDirection.Output;
                        //cmd.Parameters.Add(para);
                        ret_code = TinyDAL.Exec(ref cmd);
                        if (ret_code == 0)
                        {
                            lblErrOri.Text = "Done.";
                        }
                        else if (ret_code == 1)
                        {
                            lblErrOri.Text = "The EMD is not in edit mode!";
                            break;
                        }
                        else if (ret_code == 2)
                        {
                            lblErrOri.Text = "Adding failed: " + item.Value;
                            break;
                        }
                        else
                            lblErrOri.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            lblErrOri.Text = ex.Message;
                        else
                            lblErrOri.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                    }
                }
            }
            lstApplyOri.DataBind();
        }
        protected void btnDeleteDes_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrDes.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_delete_des_airport");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@airport", lstApplyDes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrDes.Text = "Done.";
                    lstApplyDes.DataBind();
                }
                else if (ret_code == 1)
                    lblErrDes.Text = "The EMD is not in edit mode!";
                else if (ret_code == 2)
                    lblErrDes.Text = "Deleting failed!";
                else
                    lblErrDes.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrDes.Text = ex.Message;
                else
                    lblErrDes.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAddAirportDes_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrDes.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            foreach (ListItem item in lstAirports2.Items)
            {
                if (item.Selected)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("bll_emd_add_des_airport");
                        SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                        para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                        para = cmd.Parameters.AddWithValue("@airport", item.Value);
                        para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                        para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                        //para = new SqlParameter("@ret_code", SqlDbType.Int);
                        //para.Direction = ParameterDirection.Output;
                        //cmd.Parameters.Add(para);
                        ret_code = TinyDAL.Exec(ref cmd);
                        if (ret_code == 0)
                        {
                            lblErrDes.Text = "Done.";
                        }
                        else if (ret_code == 1)
                        {
                            lblErrDes.Text = "EMD is not editing status!";
                            break;
                        }
                        else if (ret_code == 2)
                        {
                            lblErrDes.Text = "Adding failed: " + item.Value;
                            break;
                        }
                        else
                            lblErrDes.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            lblErrDes.Text = ex.Message;
                        else
                            lblErrDes.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                    }
                }
            }
            lstApplyDes.DataBind();
        }
        protected void btnDeleteCabin_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrCabin.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_delete_cabin");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@cabin", lstApplyToCabins.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrCabin.Text = "Done.";
                    lstApplyToCabins.DataBind();
                }
                else if (ret_code == 1)
                    lblErrCabin.Text = "This EMD is not in edit mode!";
                else if (ret_code == 2)
                    lblErrCabin.Text = "Deleting failed! Maybe the cabin was deleted before.";
                else
                    lblErrCabin.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrCabin.Text = ex.Message;
                else
                    lblErrCabin.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAddCabin_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrCabin.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_add_cabin");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@cabin", lstCabins.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrCabin.Text = "Done.";
                    lstApplyToCabins.DataBind();
                }
                else if (ret_code == 1)
                    lblErrCabin.Text = "This EMD is not in edit mode!";
                else if (ret_code == 2)
                    lblErrCabin.Text = "Adding failed! Maybe the cabin was added before.";
                else
                    lblErrCabin.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrCabin.Text = ex.Message;
                else
                    lblErrCabin.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }


        protected void btnDeleteAC_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrAc.Text = "Unauthorized!";
                return;
            }

            try
            {
                int ret_code;
                SqlCommand cmd = new SqlCommand("bll_emd_delete_aircraft");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                para = cmd.Parameters.AddWithValue("@tail_no", lstApplyAc.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                //para = new SqlParameter("@ret_code", SqlDbType.Int);
                //para.Direction = ParameterDirection.Output;
                //cmd.Parameters.Add(para);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrAc.Text = "Done.";
                    lstApplyAc.DataBind();
                }
                else if (ret_code == 1)
                    lblErrAc.Text = "The EMD is not in edit mode!";
                else if (ret_code == 2)
                    lblErrAc.Text = "Deleting failed!";
                else
                    lblErrAc.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrAc.Text = ex.Message;
                else
                    lblErrAc.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAddAc_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FD2") < 0)
            {
                lblErrAc.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            foreach (ListItem item in lstAircrafts.Items)
            {
                if (item.Selected)
                {
                    try
                    {
                        SqlCommand cmd = new SqlCommand("bll_emd_add_aircraft");
                        SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                        para = cmd.Parameters.AddWithValue("@emd_id", lblEmdID.Text);
                        para = cmd.Parameters.AddWithValue("@tail_no", item.Value);
                        para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                        para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                        //para = new SqlParameter("@ret_code", SqlDbType.Int);
                        //para.Direction = ParameterDirection.Output;
                        //cmd.Parameters.Add(para);
                        ret_code = TinyDAL.Exec(ref cmd);
                        if (ret_code == 0)
                        {
                            lblErrAc.Text = "Done.";
                        }
                        else if (ret_code == 2)
                        {
                            lblErrAc.Text = "Adding failed: " + item.Value;
                            break;
                        }
                        else if (ret_code == 1)
                        {
                            lblErrAc.Text = "EMD is not editing status!";
                            break;
                        }
                        else
                            lblErrAc.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            lblErrAc.Text = ex.Message;
                        else
                            lblErrAc.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                    }
                }
            }
            lstApplyAc.DataBind();
        }
        protected void lstAirports1_DataBound(object sender, EventArgs e)
        {
            lstAirports1.Items.Add(new ListItem("* - (All Airports) ", "*"));
        }
        protected void lstAirports2_DataBound(object sender, EventArgs e)
        {
            lstAirports2.Items.Add(new ListItem("* - (All Airports) ", "*"));
        }
        protected void lstAircrafts_DataBound(object sender, EventArgs e)
        {
            lstAircrafts.Items.Add(new ListItem("* - (All Aircrafts) ", "*"));
        }
    }
}